package com.tlxch.server.teacher.impl;

import com.tlxch.mapper.SClassMapper;
import com.tlxch.mapper.SStudentMapper;
import com.tlxch.mapper.ScGradesMapper;
import com.tlxch.pojo.SClass;
import com.tlxch.pojo.ScGrades;
import com.tlxch.server.teacher.TSclassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class TSclassServiceImpl implements TSclassService {
    @Autowired
    SClassMapper sClassMapper;
    @Autowired
    SStudentMapper sStudentMapper;
    @Autowired
    ScGradesMapper scGradesMapper;


    @Transactional
    public void addClassAndGrade(SClass sClass) {
        try {
            sClassMapper.insert(sClass);
            List<String> stuid = sStudentMapper.getStuId(sClass.getScMajor(),sClass.getScGrade(),sClass.getScClass());
            if (stuid.size() > 0){
                List<ScGrades> scGrades = new ArrayList<>();
                for (String sid : stuid){
                    ScGrades scGrades1 = new ScGrades();
                    scGrades1.setScgSid(sid);
                    scGrades1.setScgCid(sClass.getScCid());
                    scGrades.add(scGrades1);
                }
                scGradesMapper.insertlist(scGrades);
            }
        }catch (Exception e){
            throw new RuntimeException("新增课程失败",e);
        }
    }

    public List<ScGrades> getStuListByTid(String tid) {
        return sClassMapper.getStuListByTid(tid);
    }
}
